﻿package org.openChart.sprite 
{
	import flash.media.SoundLoaderContext;
	import flash.display.DisplayObject;
	import org.openChart.display.DisplayBaseObject;
	import flash.geom.Point;
	
	/**
	 * The base class of container sprite. The container means that there are other sprites in it.
	 * Some command occured in child sprite can be transfered to this container.
	 * 
	 * @author Chongyaun
	 */
	public class ContainerSprite extends BaseSprite implements CommandHandler
	{
		public var children_:Array = new Array();
		private var length_:uint;
		
		/**
		 * The constructor of container sprite.
		 * 
		 * @param	displayObject The display model of container spirte.
		 * @param	commandHander The command handler.
		 */
		public function ContainerSprite(displayObject:DisplayBaseObject, commandHander:CommandHandler)
		{
			children_ = new Array();
			//children_.push("dfsa");
			super(displayObject, commandHander);
		}
		
		/**
		 * To process the command transfered from its child sprite.
		 * 
		 * @param	command To mark which command occured.
		 * @param	target The sprite on which the command occured.
		 */
		public function processCommand( command:uint, target:BaseSprite ):void
		{
			
		}
		
		/**
		 * Process the mouse command.
		 * 
		 * @param	command The command type
		 * @param	traget The sprite on which the command occurs.
		 * @param	position The mouse position when the command occurs.
		 * @param	object The object is used to transfer some data.
		 */
		public function processMouseCommand(command:uint, target:BaseSprite, position:Point, object:Object):void
		{
			
		}
		
		public override function repaint():void
		{
			super.repaint();
			for (var i:uint = 0; i < length; i++ )
			{
				getAt(i).repaint();
			}
		}
		
		public function get length():uint
		{
			return length_;
		}
		
		public override function addChild(sprite:DisplayObject):DisplayObject
		{
			length_++;
			children_.push(sprite);
			return super.addChild(sprite) as DisplayObject;
		}
		
		public function getAt(index:uint):BaseSprite
		{
			if (index >= length_ || index < 0)
				throw new Error("Index out of boundary error!");
			return children_[index] as BaseSprite;
		}
	}
}